GBML manual / Grids

grid_get_region_sum

Adds up the cells in a block of target - from column x1, row y1 to column x2, row y2, both corners included - and gives the total, as a whole number for a grid of whole numbers and a decimal for a grid of decimals. A block with nothing in it adds up to 0. A grid of text, yes-or-no values or structs is refused when the project is checked. A total too big for the kind stops at the biggest (or smallest) number it can hold instead of wrapping round.

Syntax

grid_get_region_sum(target, x1, y1, x2, y2) -> element

Arguments

NameType
targetgrid
x1int
y1int
x2int
y2int

Returns

element

Description

Adds up the cells in a block of target, a grid, and gives you the total. The block runs from column x1, row y1 to column x2, row y2, and both corners are included: (1, 1) to (3, 2) is 6 cells.

On a grid of whole numbers the total is a whole number. On a grid of decimals it is a decimal.

var total = grid_get_region_sum(scores, 0, 0, 3, 2);

The block follows the same rules as grid_fill_region, which has the whole story. In short: both corners are included, corners the wrong way round make an empty block, a block that runs off the grid is cut to the grid, and a corner typed as a number that is off the grid is refused when the project is checked.

A block with nothing in it adds up to 0

If the corners are the wrong way round, or the block is wholly off the grid, there are no cells to add up, and the total is 0. This can only happen with numbers that are worked out while the game runs. A mistake typed straight into the code is refused when checked.

It only works on numbers

grid_get_region_sum needs a grid of whole numbers (grid_create(8, 6, 0)) or of decimals (grid_create(8, 6, 0.0)). A grid of text, of yes-or-no values, of structs or of references has nothing sensible to add up, so it does not build: the project is refused when it is checked, with a message that says what the grid holds. It is a message from GBML, not an error from the compiler later on.

What if the total is too big

A whole number in GBML is a 32-bit number, which holds from -2147483648 up to 2147483647. A decimal holds from about -524288 up to about 524287.99. A total over a big block of big numbers can be more than that, and the Game Boy Advance cannot hold a bigger number.

What happens is that the total stops at the biggest (or smallest) number the grid's kind can hold. It does not wrap round to some wrong number like a car's odometer rolling over. So adding 2000000000 and 2000000000 gives 2147483647, not a negative number.

Two details:

If your numbers can really get that big, add up a smaller block, or divide the cells down before you add.

The grid has to know its size

Like grid_fill_region, it needs to know how many cells are in a row. A grid with no size written down, or a script's grid parameter, is refused. Do the adding in the object that made the grid and hand the script the answer.

What happens in the awkward cases

You do thisWhat happens
use it on a grid of text, yes-or-no, structs or referencesrefused when checked
give a corner with a decimal point, like 1.5refused when checked. A corner is a whole number
give it a list or an arrayrefused when checked
give it the wrong number of argumentsrefused when checked
a block that runs off the gridonly the cells on the grid are added

What it costs

It visits every cell in the block once, so the time grows with the number of cells in the block. It does not change the grid.

Example

var scores = grid_create(4, 3, 0);
grid_fill_region(scores, 0, 0, 1, 1, 5);                  // a 2 by 2 block of 5s
var total = grid_get_region_sum(scores, 0, 0, 3, 2);      // 4 cells of 5 = 20
var part = grid_get_region_sum(scores, 0, 0, 0, 0);       // just one cell: 5

On the GBA

Two loops over the block - O(cells in the block). The total is worked out exactly in 64 bits and only then held to the range the grid's kind has (an int, 32 bits: -2147483648 to 2147483647; a real, a fixed-point number: about -524288 to 524287.99), so a sum that does not fit stops at the end of the range rather than wrapping to a wrong number. It is held once at the end, not on every step: INT_MAX + INT_MAX + INT_MIN is 2147483646. It needs the grid's width, which a flat vector does not carry, so it takes the width and height the checker reads from the grid's declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes them into the C++ as plain numbers - the same two numbers [# x, y] uses. A grid with none (a script's parameter, one declared with no shape) is refused. The four numbers are worked out once each, in the order written, then held to 0..width-1 and 0..height-1, so the loops cannot leave the grid and a block that runs off one edge never wraps onto the next row; the flat index is also checked against the vector's real size. Numbers typed straight into the call that are off the grid, or corners typed the wrong way round, are refused when the project is checked. Only int and real grids build: there is nothing sensible to add up in text.

See also: grid_get_region_maxgrid_get_region_meangrid_fill_regiongrid_widthgrid_heightgrid_createGridsData containers